// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: Li Zhaoni 17789284585@163.com
 * @Date: 2024-08-01 19:28:24
 * @LastEditors: wxc superjava@qq.com
 * @LastEditTime: 2024-09-29 13:05:15
 * @FilePath: /SGFEM/DBManager/src/Repository/HDF5/Material/MaterialSummaryRepository.cpp
 */

#include "DBManager/Repository/HDF5/Input/Material/MaterialSummaryRepository.h"

#include <algorithm>
#include <cstddef>
#include <set>
#include <vector>

#include "DataStructure/Common/Id.h"
#include "DataStructure/Common/Integer.h"
#include "DataStructure/Common/Status.h"
#include "DataStructure/Input/Material/MaterialTypes.h"

#include "DBManager/Database/HDF5Database.h"
#include "DBManager/IRepository/Input/Material/IMaterialSummaryRepository.h"
#include "DBManager/Repository/HDF5/Base/HDF5RepositoryMacro.h"
#include "DBManager/Repository/HDF5/Base/SingleRepositoryBase.h"

HDF5_REG_REPOSITORY (MaterialSummaryRepository)

using namespace SG::DBManager::HDF5;

using namespace SG::DBManager;
using namespace SG::DataStructure::FEM;
MaterialSummaryRepository::MaterialSummaryRepository (HDF5Database& database)
    : SingleSetRepositoryBase<SummaryInfo<SG::DataStructure::FEM::MaterialTypes>> (database)
{}

void MaterialSummaryRepository::InitialDatasetInfo ()
{
    RegisterTableInfo<SG::DataStructure::Common::Id> ("/OWN/INDEX/INPUT/MATERIAL/MATSUMMARY", IndexDataset);
    RegisterTableInfo<SummaryInfo<SG::DataStructure::FEM::MaterialTypes>> ("/OWN/INPUT/MATERIAL/MATSUMMARY", MainDataset);
    CreateDatasetInFile ();
}

void MaterialSummaryRepository::InitDataset ()
{
    SingleSetRepositoryBase<SummaryInfo<SG::DataStructure::FEM::MaterialTypes>>::InitDataset ();

    auto insertData = [=] () {
        // 触发持久化信号
        GetDatabase ().GetHDF5Hub()->m_materialSummarySignal ();

        // 便利所有index表，进行数据的读取
        for (auto&& i : GetDatabase ().GetHDF5Hub()->m_materialTypeWithIndexPathMapping)
        {
            if (GetDatabase ().GetFile ()->exist (i.second))
            {
                std::vector<SG::DataStructure::Common::Id> idList;
                GetDataset (i.second).read (idList);

                std::vector<SummaryInfo<SG::DataStructure::FEM::MaterialTypes>> info;

                info.reserve (idList.size ());
                for (size_t index = 0; index < idList.size (); index++)
                {
                    info.emplace_back (idList[index], i.first, index);
                }

                Add (info);
            }
        }
    };

    if (GetMainDatasetInfo ().GetRowCount () == 0)
    {
        insertData ();
        SortData ();
    }
}

std::vector<SG::DataStructure::FEM::MaterialTypes> MaterialSummaryRepository::GetMaterialType (SG::DataStructure::Common::I64 mId)
{
    std::vector<SG::DataStructure::FEM::MaterialTypes> result;

    std::vector<SummaryInfo<SG::DataStructure::FEM::MaterialTypes>> matInfos;  // NOLINT(misc-const-correctness)

    if (FindBySId (mId, matInfos) == SG::DataStructure::Common::Status::STATUS_Success)
    {
        for (auto&& i : matInfos)
        {
            result.emplace_back (i.m_type);
        }
    }

    return result;
}
std::vector<SG::DataStructure::FEM::MaterialTypes> MaterialSummaryRepository::GetAllMaterialType ()
{
    // 获取所有键值
    std::vector<SummaryInfo<SG::DataStructure::FEM::MaterialTypes>> sums;
    sums.reserve (GetMainDatasetInfo ().GetRowCount ());
    GetDataset (MainDataset).read (sums);

    // 使用 set 存储不同的 m_type 值
    std::vector<SG::DataStructure::FEM::MaterialTypes> types;

    // 便利所有index表，进行数据的读取
    for (auto&& i : GetDatabase ().GetHDF5Hub()->m_materialTypeWithIndexPathMapping)
    {
        if (GetDatabase ().GetFile ()->exist (i.second))
        {
            if (GetDataset (i.second).getElementCount () > 0)
                // 将 m_type 插入到 set 中
                types.emplace_back (i.first);
        }
    }

    return types;
}

std::vector<SG::DataStructure::Common::Id> MaterialSummaryRepository::GetAllMaterialID ()
{
    std::vector<SG::DataStructure::Common::Id> allId;
    this->GetDataset (IndexDataset).read (allId);

    return allId;
}
